﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using SharepointSolutionInstaller.Resources;
using SharepointSolutionInstaller.Utils.Configuration;


namespace SharepointSolutionInstaller.Utils.RequiredChecks
{
    internal class SolutionCheck : SystemCheck
    {
        private SPSolution solution;
        Guid solutionId = Guid.Empty;
        string solutionTitle;

        internal SolutionCheck(Guid solutionId, string solutionTitle)
            : base("SolutionCheck")
        {
            this.solutionId = solutionId;
            this.solutionTitle = solutionTitle;
        }

        protected override SystemCheckResult DoExecute()
        {
            try
            {
                solution = SPFarm.Local.Solutions[solutionId];
                if (solution != null)
                {
                    this.OkText = InstallConfiguration.FormatString(CommonUIStrings.solutionCheckOkTextInstalled, solutionTitle);
                }
                else
                {
                    this.OkText = InstallConfiguration.FormatString(CommonUIStrings.solutionCheckOkTextNotInstalled, solutionTitle);
                }
            }

            catch (NullReferenceException)
            {
                throw new Exception(CommonUIStrings.installExceptionDatabase);
            }

            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }

            return SystemCheckResult.Success;
        }

        protected override bool CanRun
        {
            get { return IsWSSInstalled; }
        }

        internal SPSolution Solution
        {
            get { return solution; }
        }
    }
}
